package com.client.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.websocket.server.PathParam;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.client.pojo.Comment;
import com.client.service.CommentService;



@Controller
public class CommentController {
	@Autowired
	private CommentService commentService;
	//添加评论
	@RequestMapping("/insertComment")
	public String selectCom(HttpServletRequest request){
		String postId=request.getParameter("postId");
		Integer pId=Integer.parseInt(postId);
		String username=request.getSession().getAttribute("username")+"";
		String comment=request.getParameter("comment");
		Date date=new Date();
		System.err.println(username+"1222222222222");
		commentService.insertCom(username, pId, comment, date);
//		commentService.insertCom(username, pId, comment, date);
//		return "forword:/selectCom?id="+postId;
		return "redirect:selectCom?postId="+postId;
		
	}
	
	//查看评论
	@RequestMapping("/selectCom")
	public String selectCom(@PathParam("postId")String postId,Model model,HttpServletRequest request){
//		Integer id=Integer.parseInt(postId);
//		JSONArray jsonArr=new JSONArray();
//		
//		List<Comment>comlist=commentService.selectCom(id);
		
//		StringBuffer str=new StringBuffer();
//		str.append("[");
//		if(comlist!=null){
//			for(Comment c:comlist){
//				str.append("{");
//				str.append("id:"+c.getId()+",");
//				str.append("img:"+"\""+"./images/img.jpg"+"\",");
//				str.append("replyName:"+"\""+c.getNickname()+"\",");
//				str.append("beReplyName:"+"\""+c.getNickname()+"\",");
//				str.append("content:"+"\""+c.getContent()+"\",");
//				str.append("time:"+"\""+"2017-10-17 11:42:53"+"\",");
//				str.append("address:"+"\""+"深圳"+"\",");
//				str.append("osname:"+"\""+"win8.1"+"\",");
//				str.append("browse:"+"\"谷歌"+"\",");
//				str.append("replyBody:[]");
//				str.append("},");				
//			}
//			
//			System.out.println(str.toString());
//			str.deleteCharAt(str.length()-1);
//			str.append("]");
//			System.out.println(str.toString());
//			model.addAttribute("jsonArr",str.toString());
//			return "show";
//		}
//		return "show";
		Integer id=Integer.parseInt(postId);
		List<Comment>comlist=commentService.selectCom(id);
		
		HttpSession ss=request.getSession();
		String username=ss.getAttribute("username")+"";
		model.addAttribute("postId", postId);
		model.addAttribute("username", username);
		if(comlist!=null){
			model.addAttribute("comlist",comlist);
			
			return "comment";
		}
		return "comment";
		
	}
	
	
}
